// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import App from './App'
import store from './store'
import router from './router'
import axios from 'axios'
import 'common/scss/reset.scss'

import echarts from 'echarts'
Vue.prototype.$echarts = echarts 

import NProgress from 'nprogress';
import 'nprogress/nprogress.css';


Vue.prototype.$axios=axios;
Vue.use(ElementUI)
Vue.config.productionTip = false

NProgress.inc(0.5)
NProgress.configure({ easing: 'ease', speed: 200, showSpinner: false })

var isPush=0;

router.beforeEach((to, from, next) => {
  NProgress.start();
  if(store.state.user.user){
    if(to.path=="/login"){
      // next({path:'/'})
      next()
    }else{
      if(isPush==0){
        const roles=store.getters.roles;
        store.dispatch('GenerateRoutes', { roles }).then(() => { // 生成可访问的路由表
          router.addRoutes(store.getters.addRouters) // 动态添加可访问路由表
          next();
          // next({ ...to, replace: false }) // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
        })
        isPush++;
      }else{
        next();
      }
     
    }

  }else{
    console.log(to.path)
    if (to.path === '/login') { //这就是跳出循环的关键
        next()
    } else {
      next('/login')
    }
  }
})
router.afterEach(transition => {
    NProgress.done();
});
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})
